package com.work.formaldehyde.activityadapter;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.work.formaldehyde.R;
import com.work.formaldehyde.activity.TuiKuanJinDuActivity;
import com.work.formaldehyde.model.KePuListModel;
import com.work.formaldehyde.model.TuiHuoModel;
import com.work.formaldehyde.util.ApiUtils;

import java.util.ArrayList;

public class TuiHuoListAdapter extends BaseAdapter {
    ArrayList<TuiHuoModel.data> list;
    Context context;
    Activity activity;

    public TuiHuoListAdapter(ArrayList<TuiHuoModel.data> list, Context context, Activity activity) {
        // TODO Auto-generated constructor stub
        this.list = list;
        this.activity = activity;
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list != null ? list.get(position) : 0;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        convertView = LayoutInflater.from(context).inflate(R.layout.list_tuihuo_item, null);
        init i = new init();
        i.peisongdizhi = convertView.findViewById(R.id.peisongdizhi);
        i.shangpingmingzi = convertView.findViewById(R.id.shangpingmingzi);
        i.gouwuche_img = convertView.findViewById(R.id.gouwuche_img);
        i.gouwuche_manjian_text = convertView.findViewById(R.id.gouwuche_manjian_text);
        i.quzhifu = convertView.findViewById(R.id.quzhifu);
        i.head = convertView.findViewById(R.id.head);
        i.shangpingmingzi.setText(list.get(position).getGoods().get(0).getGoodsinfo().getGoods_name());
        i.gouwuche_manjian_text.setText(list.get(position).getGoods().get(0).getGoodsinfo().getGoods_discount_price()+"元");
        if (list.get(position).getReturn_status().equals("0"))
            i.peisongdizhi.setText("用户申请");
        if (list.get(position).getReturn_status().equals("1"))
            i.peisongdizhi.setText("商家同意退款");
        if (list.get(position).getReturn_status().equals("2"))
            i.peisongdizhi.setText("退款成功");
        if (list.get(position).getReturn_status().equals("3"))
            i.peisongdizhi.setText("商家拒绝");
        ApiUtils.GET_IMAGE(context, list.get(position).getGoods().get(0).getGoodsinfo().getGoods_img(), i.gouwuche_img);
        if (list.get(position).getGoods().size() > 1) {
            for (int creview = 1; creview < list.get(position).getGoods().size(); creview++) {
                LinearLayout relativeLayout = new LinearLayout(context);
                LinearLayout relativeLayouts = new LinearLayout(context);
                ImageView imageView = new ImageView(context);
                TextView textView = new TextView(context);
                TextView textViews = new TextView(context);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                LinearLayout.LayoutParams lps = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                LinearLayout.LayoutParams lpimg = new LinearLayout.LayoutParams(i.gouwuche_img.getLayoutParams().width, i.gouwuche_img.getLayoutParams().height);
                LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                LinearLayout.LayoutParams lp1s = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lps.setMargins(24, 0, 0, 0);
                lpimg.setMargins(14, 0, 0, 0);
                lp.setMargins(14, 14, 0, 0);
                lp1s.setMargins(4, 4, 4, 4);
                lp1.setMargins(0, 12, 0, 0);
                relativeLayout.setLayoutParams(lp);
                relativeLayout.setOrientation(LinearLayout.HORIZONTAL);
                relativeLayouts.setLayoutParams(lps);
                relativeLayouts.setOrientation(LinearLayout.VERTICAL);
                imageView.setLayoutParams(lpimg);
                ApiUtils.GET_IMAGE(context, list.get(position).getGoods().get(creview).getGoodsinfo().getGoods_img(), imageView);
                textView.setLayoutParams(lp1);
                textView.setLines(2);
                textView.setEllipsize(TextUtils.TruncateAt.END);
                textView.setText(list.get(position).getGoods().get(creview).getGoodsinfo().getGoods_name());
                textView.setTextColor(context.getResources().getColor(R.color.text_black));
                textView.setTextSize(14);
                textViews.setLayoutParams(lp1);
                textViews.setText("¥" + list.get(position).getGoods().get(creview).getGoodsinfo().getGoods_discount_price());
                textViews.setTextColor(context.getResources().getColor(R.color.home_bottom_text_color_ischeck));
                textViews.setTextSize(11);
                relativeLayout.addView(imageView);
                relativeLayout.addView(relativeLayouts);
                relativeLayouts.addView(textView);
                relativeLayouts.addView(textViews);
                i.head.addView(relativeLayout);
            }
        }
        i.quzhifu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent tokpxq = new Intent(context, TuiKuanJinDuActivity.class);
                tokpxq.putExtra("gettype", list.get(position).getReturn_status());
                tokpxq.putExtra("tuikuanjintext", "1");
                tokpxq.putExtra("shangpingmingzi", list.get(position).getGoods().get(0).getGoodsinfo().goods_name);
                tokpxq.putExtra("tkyy", list.get(position).getReturn_cause());
                tokpxq.putExtra("tkje", list.get(position).getReturn_money());
                tokpxq.putExtra("sqsj", list.get(position).getReturn_time());
                tokpxq.putExtra("tkbm", list.get(position).getReturn_number());
                tokpxq.putExtra("cause", list.get(position).getCause());
                tokpxq.putExtra("tksj", list.get(position).getMerchant_time());
                tokpxq.putExtra("gouwuche_img", list.get(position).getGoods().get(0).getGoodsinfo().getGoods_img());
                context.startActivity(tokpxq, ActivityOptions.makeSceneTransitionAnimation(activity).toBundle());
            }
        });
        convertView.setTag(i);
        return convertView;
    }

    public static class init {
        //首页list的子控件
        TextView peisongdizhi, shangpingmingzi, gouwuche_manjian_text;
        ImageView gouwuche_img;
        public RelativeLayout quzhifu;
        LinearLayout head;
    }
}

